/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ua.yandex.skipass;

import java.util.GregorianCalendar;
import ua.yandex.collections.MyArrayList;
import ua.yandex.collections.MyLinkedList;
import ua.yandex.collections.MyList;

/**
 *
 * @author Neophron
 */
public class SkiPassSystem {

    private final MyList turnstiles;

    private final MyList passes;

    private final MyList record;

    private int lastUsedId = -1;

    private final PassCreator creator;

    public SkiPassSystem(PassCreator creator) {
        turnstiles = new MyLinkedList();
        passes = new MyArrayList();
        record = new MyLinkedList();
        this.creator = creator;
    }

    public void createPass(PassType type, Enum arg) {
        lastUsedId++;
        Pass newPass = creator.createPass(lastUsedId, type, arg);
        Pair p = new Pair(newPass, false);
        passes.add(p);
    }

    public Pass getPass(int id) {
        return (Pass) ((Pair) passes.get(id)).getFirst();
    }

    public void blockPass(int id) {
        if (id > lastUsedId) {
            return;
        }
        ((Pair) passes.get(id)).setSecond(true);
    }

    public boolean isPassBlocked(int id) {
        if (id > lastUsedId) {
            return true;
        }
        return (boolean) ((Pair) passes.get(id)).getSecond();
    }

    public void passed(int id) {
        if (id > lastUsedId) {
            return;
        }
        Pair p = new Pair(id, true);
        record.add(p);
    }

    public void notPassed(int id) {
        if (id > lastUsedId) {
            return;
        }
        Pair p = new Pair(id, false);
        record.add(p);
    }

    public MyList getData() {
        return record;
    }

    public MyList getData(PassType type) {
        MyList res = new MyLinkedList();
        for (int i = 0; i < record.size(); i++) {
            int id = (int) ((Pair) record.get(i)).getFirst();
            if (((Pass) ((Pair) passes.get(id)).getFirst()).getType() == type) {
                res.add(record.get(i));
            }
        }
        return res;
    }

    public void newTS() {
        Turnstile ts = new Turnstile(this, new GregorianCalendar());
        turnstiles.add(ts);
    }

    public Turnstile getTS(int id) {
        return (Turnstile) turnstiles.get(id);
    }

    public int countOfPass() {
        return lastUsedId + 1;
    }

}
